package com.metro.paperless.common.controller;

import com.metro.paperless.common.beans.vo.WebUserInfo;
import com.metro.paperless.sys.beans.vo.SysPermission;
import com.metro.paperless.sys.beans.vo.SysRole;
import com.metro.paperless.sys.service.inf.ISysPermissionService;
import com.metro.paperless.sys.service.inf.ISysRoleService;
import com.metro.paperless.web.controller.BaseController;
import com.metro.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 后台首页控制器
 * 
 * @author
 **/
@Controller()
@RequestMapping(value = "/index")
public class IndexController extends BaseController {

	//角色
	@Autowired
	private ISysRoleService sysRoleService;
	
	//权限
	@Autowired
	private ISysPermissionService sysPermissionService;
	/**
	 * 系统首页
	 * 
	 * @author xk
	 */
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String home(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
	    Subject currentUser = SecurityUtils.getSubject();
        WebUserInfo userInfo  = (WebUserInfo)currentUser.getPrincipal();
		List<SysRole> roleList = sysRoleService.queryRoleByUserId(userInfo.getUserId());
		//2.用户角色
		userInfo.setRoleList(roleList);
		//3.权限
		String roleIds = userInfo.getRoleIds();
		if(StringUtils.isNotEmpty(roleIds)){
			List<SysPermission> menuList = sysPermissionService.queryMenuByRoleIds(roleIds);
			userInfo.setPermissionList(menuList);
		}
		model.addAttribute("menuList", userInfo.getPermissionList());
		return "/portal/home";
	}

	/**
	 * 用户工作台
	 * 
	 * @author xk
	 */
	@RequestMapping(value = "/default", method = RequestMethod.GET)
	public ModelAndView bench(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		ModelAndView mav = new ModelAndView();
        model.put("beginTime", DateUtil.formatDate(DateUtil.addDate(new Date(), -31)));
        model.put("endTime", DateUtil.formatDate(DateUtil.addDate(new Date(), -1)));
		mav.setViewName("/portal/welcome");
		return mav;
	}

	/**
	 * 进入tab标签
	 * @return
	 */
	@RequestMapping(value="/tab")
	public String tab(){
		return "/portal/tab";
	}

	/**
	 * waiting
	 */
	@RequestMapping(value="/waiting")
	public String waiting(){
		return "commons/error/waiting";
	}
	
	/**
	 * 默认导入页面
	 */
	@RequestMapping(value="/importData")
	public String importData(){
		return "commons/error/importData";
	}
}
